<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sortNo" :min="1" style="width: 100%" controls-position="right"></el-input-number>
			</el-form-item>
			<el-form-item label="业务标题" prop="title">
				<el-input v-model="form.title" placeholder="业务标题" clearable></el-input>
			</el-form-item>
			<el-form-item label="BPM流程" prop="flowId">
					<el-select v-model="form.flowId" clearable @change="loadBpmFlowStep" style="width: 100%">
						<el-option v-for="item in flowOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
			</el-form-item>
			<el-form-item label="执行步骤" prop="stepId" v-if="form.flowId!=''" style="width: 100%">
				<el-select v-model="form.stepId" clearable style="width: 100%">
					<el-option v-for="item in stepOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="数据源" prop="dbSourceId">
				<el-select v-model="form.dbSourceId" clearable style="width: 100%">
					<el-option v-for="item in dbOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-divider>业务SQL</el-divider>
			<sc-code-editor ref="editor" v-model="form.sqlStr" mode="sql" :height="100" theme="darcula" :options="{autoRefresh: true}"></sc-code-editor>
			<el-form-item prop="sqlStr"></el-form-item>
			<el-form-item label="是否有效">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" maxlength="150" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import {defineAsyncComponent} from "vue";

const scCodeEditor = defineAsyncComponent(() => import('@/components/scCodeEditor'));
	export default {
		components: {
			scCodeEditor
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				dbOptions:[],
				flowOptions:[],
				stepOptions:[],
				//表单数据
				form: {
					engineId:'',
					sortNo: 1,
					title:"",
					flowId:"",
					stepId:"",
					model:'0',
					dbSourceId:"",
					sqlStr:"",
					status: 1,
					remark: ""
				},
				//验证规则
				rules: {
					flowId: [
						{required: true, message: '请选择流程'}
					],
					stepId: [
						{required: true, message: '请选择流程步骤'}
					],
					title: [
						{required: true, message: '请输入标题名称'}
					],
					dbSourceId:[
						{required: true, message: '请选择数据源'}
					],
					sqlStr:[
						{required: true, message: 'Sql语句不能为空'}
					]
				},
			}
		},
		watch: {
			'form.flowId': {
				handler(val) {
					this.initBpmFlowStep(this.form.stepId);
				}
			},
		},
		mounted() {
			this.getBpmFlowListForSelect();
			this.getDbSourceList();
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				if(this.mode=="add")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmEngine.insert.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}else if(this.mode=="edit")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.bpm.bpmEngine.update.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}
			},
			async getDbSourceList()
			{
				var res = await this.$API.system.dbSourceConfig.getDbSourceListForSelect.get();
				if(res.code == 200){
					this.dbOptions = res.data;
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			async getBpmFlowListForSelect()
			{
				var res = await this.$API.bpm.bpmFlow.getBpmFlowListForSelect.get({model:'0'});
				if(res.code == 200){
					this.flowOptions = res.data;
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			async loadBpmFlowStep()
			{
				this.form.stepId='';
				if(this.form.flowId!='')
				{
					var res = await this.$API.bpm.bpmStep.getBpmFlowStepByFlowIdForSelect.get({flowId:this.form.flowId});
					if(res.code == 200){
						this.stepOptions = res.data;
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			},
			async initBpmFlowStep(stepId)
			{
				this.form.stepId='';
				if(this.form.flowId!='')
				{
					var res = await this.$API.bpm.bpmStep.getBpmFlowStepByFlowIdForSelect.get({flowId:this.form.flowId});
					if(res.code == 200){
						this.stepOptions = res.data;
						this.form.stepId=stepId
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			},
			async setData(data){
				this.form.sqlStr = data.sqlStr
				this.form.engineId = data.engineId
				this.form.sortNo = data.sortNo
				this.form.title = data.title
				this.form.flowId = data.flowId
				this.form.stepId = data.stepId
				this.form.dbSourceId = data.dbSourceId
				this.form.model = data.model
				this.form.status = data.status
				this.form.remark = data.remark

			}
		}
	}
</script>

<style>
</style>
